<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" :append-to-body="true" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="风险状态" prop="riskstatus" :rules="rules.validateRequire">
            <el-select v-model="model.riskstatus" filterable clearable>
              <el-option v-for="item in codeModel.riskstatusList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="风险趋势" prop="risktrend" :rules="rules.validateRequire">
            <el-select v-model="model.risktrend" filterable clearable>
              <el-option v-for="item in codeModel.risktrendList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="风险反馈" prop="riskfeedback" :rules="rules.validateRequire">
            <el-input v-model="model.riskfeedback" type="textarea" maxlength="1000" show-word-limit :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" maxlength="1000" show-word-limit :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <tl-approval-history ref="history" sourceType="MpmProjectRiskFeedback" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading || submitLoading" v-if="!disableEditSubmit" :disabled="disableSave"><svg-icon icon-class="save" />保存</el-button>
      <el-button type="primary" @click="projectSubmit" :loading="submitLoading || saveLoading" v-if="!disableEditSubmit" :disabled="disableSave"><svg-icon icon-class="send" />提交审批</el-button>
      <tl-attachment sourceType="MpmProjectRiskFeedback" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory"><svg-icon icon-class="list" />审批历史</el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval"><svg-icon icon-class="undo" />取消审批</el-button>
    </div>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit_edit } from '../../../mixins/mixin_approval_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit_edit],
  components: {},
  data() {
    return {
      moduleTitle: '项目风险反馈',
      urlPrefix: '/mpm/projectRiskFeedback/',
      url: {
        // 额外的url
      },
      riskid: 0,
      riskdescription: ''
    }
  },
  props: {
    projectinfo: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
          this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        }
        this.$set(this.model, 'orgcode', this.org.code)
        this.$set(this.model, 'disporgcode', this.org.name)
        this.$set(this.model, 'orgname', this.org.name)
        this.$set(this.model, 'deptcode', this.dept.code)
        this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'projectcode', this.projectinfo.projectcode)
        this.$set(this.model, 'riskid', this.riskid)
        this.$set(this.model, 'riskdescription', this.riskdescription)
      }
    },
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    projectSubmit() {
      let that = this
      that.submitLoading = true
      that.$refs.form.validate(async (valid, res) => {
        if (valid) {
          let saveresult = await that.addBase()
          if (saveresult) {
            this.saveLoading = true
            putAction(that.url.submit, that.model)
              .then((res) => {
                if (res.success) {
                  that.queryData(that.model.id).then((_) => {
                    that.loaded()
                    that.saveLoading = false
                    that.submitLoading = false
                  })
                  that.$tap.alert('提交审批成功', 'success')
                } else {
                  that.$tap.alert(res.message, 'warning')
                  that.saveLoading = false
                  that.submitLoading = false
                }
              })
              .catch((err) => {
                that.saveLoading = false
                that.submitLoading = false
              })
          }
        } else {
          that.submitLoading = false
          that.$tap.alert('按要求填写表单数据项，包括必填项，数据格式等')
        }
      })
    }
  }
}
</script>
